/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlet;

import Utils.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Ihsan Younes
 */
@WebServlet(name = "Servlet_Supervisor", urlPatterns = {"/Corsi_Docente","/Advisor"})
public class Servlet_Supervisor extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        
        User user = (User) request.getSession().getAttribute("user");
        
        if(user == null){
            response.sendRedirect("index.jsp?error= Prima di accedere è necessario fare il login"); 
            return ;
        }
        Docente docente = (Docente) Query_Docente.query_dati_docente(user.getName(),user.getId());
        Advisor advisor = (Advisor) Query_Advisor.query_dati_advisor(user.getName(),user.getId());
        

        
        String contex = request.getRequestURI();
        if(contex.equals("/Sito_ingegneria/Corsi_Docente")){
            
            
            String id = request.getParameter("ID");
            int ID = -1;
            if(id!=null){
                ID = Integer.parseInt(id);
                String student = request.getParameter("student");
                if(student!=null){
                    int studente = Integer.parseInt(student);
                    String operazione = request.getParameter("operazione");
                    if(operazione.contentEquals("OK")){
                        String approvato = request.getParameter("approved");
                        String voto = request.getParameter("voto");
                        Query_Docente.query_inserisci_voto(user.getName(), ID, studente,approvato,voto);
                        
                    }
                }
                
            }
        
            
            List <Corsi_tenuti_docente> corsi = new ArrayList<Corsi_tenuti_docente>();
            if(docente.getCorsi()!=null)
            corsi.addAll(docente.getCorsi().values());
        
            Corsi_tenuti_docente corso = null;
             List <Studenti_Corsi_Iscrizioni> studenti = new ArrayList<Studenti_Corsi_Iscrizioni>(); 
            if(ID>=1){
            
                corso = docente.getCorso(ID);
            
                if(corso.getStudenti() != null)
                    studenti.addAll(corso.getStudenti().values());
            
            }
        
            request.setAttribute("corsi", corsi);
            request.setAttribute("Dettagli_corsi", corso);
            request.setAttribute("studenti", studenti);
            request.setAttribute("docente", docente.isCan_be_advisor());
        
            request.getRequestDispatcher("Corsi_Docente.jsp").forward(request, response);
        }else
         if(contex.equals("/Sito_ingegneria/Advisor")){
                          
            StudyPlan plan1 = null;
            StudyPlan plan2 = null;
            StudyPlan plan3 = null;
             
             
            String id = request.getParameter("ID");
            int ID ;
            if(id!=null){
                ID = Integer.parseInt(id);
                
                
                String modifica = request.getParameter("voto_corso");
                if(modifica!=null){
                    if(request.getParameter("operazione").contentEquals("OK")){
                        int corso = Integer.parseInt(request.getParameter("corso"));
                        String voto = request.getParameter("voto");
                        String approvato = request.getParameter("approved");
                        if(modifica.contentEquals("voto")){
                            Query_Advisor.query_modifica_voto(user.getName(),user.getId(), corso, id,approvato ,voto);
                        }else{
                            Query_Advisor.query_approva_corso(user.getName(),user.getId(), corso, id,approvato);
                        }
                        advisor = (Advisor) Query_Advisor.query_dati_advisor(user.getName(),user.getId());
                    }
                }

                Iterator i = advisor.getStudenti().iterator();
                Studente s = null;
                boolean stop = false;
                while((i.hasNext())&&(stop==false)){
                    s = (Studente) i.next();
                    if(Integer.parseInt(s.getMatricola())== ID){
                        stop = true;
                    }
                }
                plan1 = s.getPlan_anno1();
                plan2 = s.getPlan_anno2();
                plan3 = s.getPlan_anno3();
        
            }

        
        
        
            List <Studente_base> lista_studenti = new ArrayList<Studente_base>();
            Iterator i = advisor.getStudenti().iterator();
            Studente s;
            Studente_base sB;
            while(i.hasNext()){
                s = (Studente) i.next();
                sB = new Studente_base();
                sB.setMatricola(s.getMatricola());
                sB.setCognome(s.getCognome());
                sB.setNome(s.getNome());
                lista_studenti.add(sB);
            }
       
       
            request.setAttribute("studyPlan1", plan1); 
            request.setAttribute("studyPlan2", plan2);
            request.setAttribute("studyPlan3", plan3);
            request.setAttribute("studenti", lista_studenti);
        
            request.getRequestDispatcher("Advisor.jsp").forward(request, response);
        }
        
        
        
    }
    
    

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
